﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using CNVP.Config;
using CNVP.Framework.Helper;
using System.Data;
using System.Data.SqlClient;
using CNVP.Framework.Utils;
using System.Web;
using CNVP.Framework;

namespace CNVP.JShop.Publish
{
    /// <summary>
    /// 解析类
    /// </summary>
    public partial class LabelMass
    {
        #region "标签属性"
        /// <summary>
        /// 标签值
        /// </summary>
        private string _LabelContent = string.Empty;
        /// <summary>
        /// 循环函数
        /// </summary>
        private string _Pattern = @"<!--for-->(?<content>[\s\S]*?)<!--/for-->";
        /// <summary>
        /// 初始值
        /// </summary>
        private string _InitialStr = string.Empty;
        /// <summary>
        /// 循环的主题
        /// </summary>
        private string _LoopStr = string.Empty;
        /// <summary>
        /// 替换值
        /// </summary>
        private string _ReplaceStr = string.Empty;
        /// <summary>
        /// 数据库前缀
        /// </summary>
        private string Prefix = DBConfig.Prefix;
        /// <summary>
        /// 解析标签
        /// </summary>
        private void InitialLabel()
        {
            _LabelContent = this.Mass_Inserted;
            _InitialStr = this.Mass_Inserted;
            _LoopStr = this.Mass_Inserted;
            Regex reg = new Regex(_Pattern, RegexOptions.Compiled);
            Match m = reg.Match(_LabelContent);
            if (m.Success)
            {
                _InitialStr = m.Groups[0].Value;
                _LoopStr = m.Groups[1].Value;
            }
        }
        #endregion
        #region "静态单元"
        /// <summary>
        /// 返回静态单元的值
        /// </summary>
        /// <returns></returns>
        private string Analyse_Dynamic()
        {
            InitialLabel();
            return _LabelContent;
        }
        #endregion

        #region "当前位置"
        /// <summary>
        /// 返回当前位置
        /// </summary>
        /// <returns></returns>
        protected string Analyse_Position(int ColumnID)
        {
            InitialLabel();
            string ShowRoot = this.GetParamValue("NT:ShowRoot");
            _ReplaceStr = CommonData.GetColumnPath(_LoopStr, ColumnID, ShowRoot);
            string _Str = string.Empty;
            if (!string.IsNullOrEmpty(_InitialStr))
            {
                _Str=_LabelContent.Replace(_InitialStr, _ReplaceStr);
            }
            return _Str;
        }
        #endregion
        #region "栏目导航"
        /// <summary>
        /// 栏目导航
        /// </summary>
        /// <returns></returns>
        private string Analyse_SiteNav(int ColumnID)
        {
            InitialLabel();
            bool ShowDiv = Convert.ToBoolean(this.GetParamValue("NT:ShowDiv"));
            string ShowCol = this.GetParamValue("NT:ShowCol");
            if (string.IsNullOrEmpty(ShowCol)) { ShowCol = "1"; }
            string ShowRoot = this.GetParamValue("NT:ShowRoot");
            string _ColumnID = this.GetParamValue("NT:ColumnID");
            string columnType = this.GetParamValue("NT:ColumnType");

            Int32 cc = 0;

            if (!String.IsNullOrEmpty(ShowRoot) && Public.IsNumber(ShowRoot))
            {
                if (ShowRoot == "5" || ShowRoot == "6")
                {
                    cc = Convert.ToInt32(_ColumnID);
                }
                if (ShowRoot == "2" || ShowRoot == "3" || ShowRoot == "4" )
                {
                    cc = ColumnID;
                }
            }



            DataTable dt = CommonData.GetAllColumn(columnType);

            DataRow[] dra = CommonData.GetColumn(dt, ShowRoot, cc, ShowCol);

            StringBuilder Str = new StringBuilder();
            if (dra != null)
            {
                foreach (DataRow dr in dra)
                {
                    String _str = _LoopStr;
                    if (Regex.IsMatch(_str, @"(<!--subfor-->(?<attributes>[\s\S]*?)<!--/subfor-->)"))
                    {
                        String subTemp = GetSubForString(_str);
                        _str = Regex.Replace(_str, @"<!--subfor-->" + subTemp + @"<!--/subfor-->", SubColumnLink(dt, Convert.ToInt32(dr["ID"]), subTemp), RegexOptions.IgnoreCase);
                    }

                    _str = _str.Replace("{#ColumnName}", dr["ColumnName"].ToString());
                    _str = _str.Replace("{#ColumnID}", dr["ID"].ToString());
                    string _strBlank = string.Empty;

                    if (!string.IsNullOrEmpty(dr["ColumnUrl"].ToString()))
                    {
                        _str = _str.Replace("{#ColumnUrl}", string.Format("{0}{1}", dr["ColumnUrl"].ToString(), _strBlank));
                    }
                    else
                    {
                        if (dr["ColumnType"].ToString() == "3")
                        {
                            _str = _str.Replace("{#ColumnUrl}", string.Format("{0}ProductList_{1}.aspx", UIConfig.InstallDir, dr["ID"].ToString()));
                        }
                        else if (dr["ColumnType"].ToString() == "2")
                        {
                            _str = _str.Replace("{#ColumnUrl}", string.Format("{0}NewsList_{1}.aspx", UIConfig.InstallDir, dr["ID"].ToString()));
                        }
                    }
                    Str.Append(_str);
                }
            }
            string _Str = string.Empty;
            if (!string.IsNullOrEmpty(_InitialStr))
            {
                _Str = this.Mass_Inserted.Replace(_InitialStr, Str.ToString());
            }
            return _Str;
        }
        private String GetSubForString(String temp)
        {
            String forString = String.Empty;
            String startString=@"<!--subfor-->";
            String endString = @"<!--/subfor-->";

            Int32 i = temp.Length;
            Int32 j = temp.IndexOf(startString);
            Int32 k = startString.Length;
            Int32 o = temp.LastIndexOf(endString);

            forString = temp.Substring(j+k,o-j-k);
            return forString;
        }
        /// <summary>
        /// 栏目导航，递归解析子循环
        /// </summary>
        /// <param name="columnID">栏目ID</param>
        /// <param name="loopString">循环内的字符串</param>
        /// <returns>解析后的Html</returns>
        private String SubColumnLink(Int32 columnID, String loopString)
        {
            String temp = loopString;
            String loopTemp = String.Empty;

            DataTable dt = CommonData.GetChildColumnList(columnID);
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {

                    if (Regex.IsMatch(temp, @"(<!--subfor-->(?<attributes>[\s\S]*?)<!--/subfor-->)"))
                    {
                        String subTemp = GetSubForString(temp);
                        temp = Regex.Replace(temp, @"<!--subfor-->" + subTemp + @"<!--/subfor-->", SubColumnLink(Convert.ToInt32(dr["ID"]), subTemp), RegexOptions.IgnoreCase);
                    }

                    try
                    {

                        temp = temp.Replace("{#ColumnName}", dr["ColumnName"].ToString());
                        temp = temp.Replace("{#ColumnID}", dr["ID"].ToString());
                        string _strBlank = string.Empty;

                        if (!string.IsNullOrEmpty(dr["ColumnUrl"].ToString()))
                        {
                            temp = temp.Replace("{#ColumnUrl}", string.Format("{0}{1}", dr["ColumnUrl"].ToString(), _strBlank));
                        }
                        else
                        {
                            if (dr["ColumnType"].ToString() == "3")
                            {
                                temp = temp.Replace("{#ColumnUrl}", string.Format("{0}ProductList_{1}.aspx", UIConfig.InstallDir, dr["ID"].ToString()));
                            }
                            else if (dr["ColumnType"].ToString() == "2")
                            {
                                temp = temp.Replace("{#ColumnUrl}", string.Format("{0}NewsList_{1}.aspx", UIConfig.InstallDir, dr["ID"].ToString()));
                            }
                        }

                    }
                    catch { }

                    loopTemp += temp;


                }
            }
            return loopTemp;
        }

        private String SubColumnLink(DataTable dt,Int32 columnID, String loopString)
        {
            String temp =String.Empty;
            String loopTemp = String.Empty;

            DataRow[] dra = dt.Select("[Depth]='" + columnID.ToString()+"'");
            if (dra != null && dra.Length > 0)
            {
                foreach (DataRow dr in dra)
                {
                    temp = loopString;
                    if (Regex.IsMatch(temp, @"(<!--subfor-->(?<attributes>[\s\S]*?)<!--/subfor-->)"))
                    {
                        String subTemp = GetSubForString(temp);
                        temp = Regex.Replace(temp, @"<!--subfor-->" + subTemp + @"<!--/subfor-->", SubColumnLink(dt,Convert.ToInt32(dr["ID"]), subTemp), RegexOptions.IgnoreCase);
                    }

                    try
                    {

                        temp = temp.Replace("{#ColumnName}", dr["ColumnName"].ToString());
                        temp = temp.Replace("{#ColumnID}", dr["ID"].ToString());
                        string _strBlank = string.Empty;

                        if (!string.IsNullOrEmpty(dr["ColumnUrl"].ToString()))
                        {
                            temp = temp.Replace("{#ColumnUrl}", string.Format("{0}{1}", dr["ColumnUrl"].ToString(), _strBlank));
                        }
                        else
                        {
                            if (dr["ColumnType"].ToString() == "3")
                            {
                                temp = temp.Replace("{#ColumnUrl}", string.Format("{0}ProductList_{1}.aspx", UIConfig.InstallDir, dr["ID"].ToString()));
                            }
                            else if (dr["ColumnType"].ToString() == "2")
                            {
                                temp = temp.Replace("{#ColumnUrl}", string.Format("{0}NewsList_{1}.aspx", UIConfig.InstallDir, dr["ID"].ToString()));
                            }
                        }

                    }
                    catch { }

                    loopTemp += temp;


                }
            }
            return loopTemp;
        }
        #endregion
        #region "通用模块"
        /// <summary>
        /// 返回通用模块
        /// </summary>
        /// <returns></returns>
        protected string Analyse_Common()
        {
            InitialLabel();
            string strSql = this.GetParamValue("NT:SQL");
            string content = this.GetParamValue("NT:Content");
            Regex c = new Regex(@"(<!--for-->(?<attributes>[^\]]*?)<!--/for-->)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            string cAtt = String.Empty;//存储属性
            string cAll = String.Empty;
            DataTable dt = new DataTable();
            try
            {
                dt = CommonData.GetCommonContent(strSql.Replace(":", ",").Replace("#", "="));
            }
            catch
            { }
            foreach (Match m in c.Matches(content.ToString()))
            {
                //获取属性
                cAtt = m.Groups["attributes"].ToString();
                cAll = m.Groups[0].Value.ToString();
                Regex r = new Regex(@"(\{#(?<attributes>[^\]]*?)\})", RegexOptions.Compiled | RegexOptions.IgnoreCase);

                string att = String.Empty;//存储属性
                string all = String.Empty;
                string tempContent = String.Empty;
                foreach (DataRow dr in dt.Rows)
                {
                    string tempCAtt = cAtt.ToString();
                    foreach (Match match in r.Matches(tempCAtt))
                    {
                        //获取属性
                        att = match.Groups["attributes"].ToString();
                        all = match.Groups[0].Value.ToString();
                        try
                        {
                            tempCAtt = tempCAtt.Replace(all, dr[att].ToString());
                        }
                        catch
                        { }
                    }
                    tempContent += tempCAtt;
                }
                try
                {
                    content = content.Replace(cAll, tempContent);
                }
                catch
                { }
            }


            return content;
        }
        #endregion
        #region "商品列表"
        /// <summary>
        /// 商品列表
        /// </summary>
        /// <param name="ColumnID">栏目ID</param>
        /// <returns></returns>
        protected string Analyse_ProductList(int ColumnID)
        {
            InitialLabel();
            string ShowRoot = !String.IsNullOrEmpty(this.GetParamValue("NT:ShowRoot")) && (Public.IsNumber(this.GetParamValue("NT:ShowRoot"))) ? this.GetParamValue("NT:ShowRoot") : "0";
            string _ColumnID = this.GetParamValue("NT:ColumnID");
            string showCol = this.GetParamValue("NT:ShowCol");
            string includeChild = this.GetParamValue("NT:IncludeChild");
            string productKey = this.GetParamValue("NT:ProductKey");
            String titleLength = this.GetParamValue("NT:TitleLength");
            String start = this.GetParamValue("NT:Start");
            Int32 cc=0;
            if (String.IsNullOrEmpty(showCol) || !Public.IsNumber(showCol))
            {
                showCol = "10";
            }

            if (!String.IsNullOrEmpty(_ColumnID) && Public.IsNumber(_ColumnID))
            {
                if (ShowRoot == "4" || ShowRoot == "3")
                {
                    cc = Convert.ToInt32(_ColumnID);
                }
                if (ShowRoot == "1" || ShowRoot == "2")
                {
                    cc = ColumnID;
                }
            }

            //起始位置确定
            Int32 i = 0;
            if (!String.IsNullOrEmpty(start) && Public.IsNumber(start))
            {
                if (Convert.ToInt32(start) > 0)
                {
                    i = Convert.ToInt32(start) - 1;
                }
            }

            DataTable Dt = new DataTable();

            //需要进一步完成功能
            Dt = CommonData.GetProductList(cc, Convert.ToInt16(showCol) + i, productKey, ShowRoot, includeChild);

            StringBuilder Str = new StringBuilder();

            if (Dt != null && Dt.Rows.Count > i)
            {
                DataRow Row = null;
                for (; i < Dt.Rows.Count; i++)
                {
                    Row = Dt.Rows[i];
                    String _str = _LoopStr;
                    _str = _str.Replace("{#ProductUrl}", String.Format("{0}ProductDetail_{1}_{2}.aspx", UIConfig.InstallDir, Convert.ToString(Row["ProductClassID"]), Convert.ToString(Row["ID"])));
                    if (!String.IsNullOrEmpty(titleLength) && Public.IsNumber(titleLength))
                    {
                        _str = _str.Replace("{#ProductName}", BasePage.CutString(Convert.ToString(Row["ProductName"]), Convert.ToInt32(titleLength)));
                    }
                    else
                    {
                        _str = _str.Replace("{#ProductName}", Convert.ToString(Row["ProductName"]));
                    }
                    _str = _str.Replace("{#FullName}", Convert.ToString(Row["ProductName"]));
                    _str = _str.Replace("{#TradePrice}", Convert.ToString(Row["TradePrice"]));
                    _str = _str.Replace("{#MarketPrice}", Convert.ToString(Row["MarketPrice"]));
                    _str = _str.Replace("{#ImgProLoad}", "/UploadFile/Product/" + Convert.ToString(Row["ImgProLoad"]));
                    Str.Append(_str);
                }
            }

            string _Str = string.Empty;
            if (!String.IsNullOrEmpty(_InitialStr))
            {
                _Str = this.Mass_Inserted.Replace(_InitialStr, Str.ToString());
            }
            return _Str;
        }
        #endregion
        #region "商品相册"
        /// <summary>
        /// 商品相册
        /// </summary>
        /// <param name="productID">商品ID</param>
        /// <returns></returns>
        protected string Analyse_ProductPic(int productID)
        {
            InitialLabel();
            string ShowRoot = this.GetParamValue("NT:ShowRoot");
            string showCol = this.GetParamValue("NT:ShowCol");
            if (String.IsNullOrEmpty(showCol) || !Public.IsNumber(showCol))
            {
                showCol = "10";
            }

            DataTable Dt = new DataTable();


            Dt = CommonData.GetProductPic(productID, Convert.ToInt16(showCol));
            StringBuilder Str = new StringBuilder();
            foreach (DataRow Row in Dt.Rows)
            {
                string _str = _LoopStr;
                _str = _str.Replace("{#SmallPic}", "/UploadFile/Product/" +Convert.ToString(Row["SmallPic"]));
                _str = _str.Replace("{#ID}", Row["ID"].ToString());
                _str = _str.Replace("{#BigPic}", "/UploadFile/Product/" + Convert.ToString(Row["BigPic"]));
                _str = _str.Replace("{#Pic}", "/UploadFile/Product/" + Convert.ToString(Row["Pic"]));
                _str = _str.Replace("{#ProductID}", Convert.ToString(Row["ProductID"]));
                Str.Append(_str);
            }
            string _Str = string.Empty;
            if (!string.IsNullOrEmpty(_InitialStr))
            {
                _Str = this.Mass_Inserted.Replace(_InitialStr, Str.ToString());
            }
            return _Str;
        }
        #endregion
        #region "商品扩展属性"
        /// <summary>
        /// 商品扩展属性
        /// </summary>
        /// <param name="productID">商品ID</param>
        /// <returns></returns>
        protected string Analyse_ProductExtendProperty(int productID)
        {
            InitialLabel();
            string ShowRoot = this.GetParamValue("NT:ShowRoot");
            DataTable Dt = new DataTable();

            Dt = CommonData.GetPropertyValue(productID); ;
            StringBuilder Str = new StringBuilder();
            foreach (DataRow Row in Dt.Rows)
            {
                string _str = _LoopStr;
                if (Row["StandardValue"].ToString() != "")
                {
                    _str = _str.Replace("{#ExtendProPertyName}", Row["ExtendProPertyName"].ToString());
                    _str = _str.Replace("{#StandardValue}", Row["StandardValue"].ToString());
                    Str.Append(_str);
                }
            }
            string _Str = string.Empty;
            if (!string.IsNullOrEmpty(_InitialStr))
            {
                _Str = this.Mass_Inserted.Replace(_InitialStr, Str.ToString());
            }
            return _Str;
        }
        #endregion
        #region "商品详细描述"
        /// <summary>
        /// 商品详细描述
        /// </summary>
        /// <param name="productID">商品ID</param>
        /// <returns></returns>
        protected string Analyse_ProductPropertyDescripttion(int productID)
        {
            InitialLabel();
            string ShowRoot = this.GetParamValue("NT:ShowRoot");
            DataTable Dt = new DataTable();

            Dt = CommonData.GetDescripttionValue(productID); ;
            StringBuilder Str = new StringBuilder();
            foreach (DataRow Row in Dt.Rows)
            {
                string _str = _LoopStr;
                if (Row["StandardValue"].ToString() != "")
                {
                    _str = _str.Replace("{#DescripttionName}", Row["DescripttionName"].ToString());
                    _str = _str.Replace("{#StandardValue}", Row["StandardValue"].ToString());
                    Str.Append(_str);
                }
            }
            string _Str = string.Empty;
            if (!string.IsNullOrEmpty(_InitialStr))
            {
                _Str = this.Mass_Inserted.Replace(_InitialStr, Str.ToString());
            }
            return _Str;
        }
        #endregion
        #region "商品规格"
        /// <summary>
        /// 商品规格
        /// </summary>
        /// <param name="productID">商品ID</param>
        /// <returns></returns>
        protected string Analyse_ProductStandard(int productID)
        {
            InitialLabel();
            string ShowRoot = this.GetParamValue("NT:ShowRoot");
            string standardValues = String.Empty;
            DataTable dts = new DataTable();
            dts = CommonData.GetStandardValue(productID);
            foreach (DataRow dr in dts.Rows)
            {
                if (String.IsNullOrEmpty(standardValues))
                {
                    standardValues =Convert.ToString(dr["StandardValueIDs"]);
                }
                else
                {
                    standardValues +=","+ Convert.ToString(dr["StandardValueIDs"]);
                }
            }
            DataTable Dt = new DataTable();

            if (String.IsNullOrEmpty(standardValues))
            {
                Dt = CommonData.GetProductStandard("0");
            }
            else
            {
                Dt = CommonData.GetProductStandard(standardValues);
            }
            StringBuilder Str = new StringBuilder();
            foreach (DataRow Row in Dt.Rows)
            {
                string _str = _LoopStr;
                _str = _str.Replace("{#ID}", Row["ID"].ToString());
                _str = _str.Replace("{#StandardValueName}", Row["StandardValueName"].ToString());
                _str = _str.Replace("{#StandardValuePic}", Row["StandardValuePic"].ToString());
                _str = _str.Replace("{#StandardName}", Row["StandardName"].ToString());
                _str = _str.Replace("{#StandardType}", Row["StandardType"].ToString());
                Str.Append(_str);
            }
            string _Str = string.Empty;
            if (!string.IsNullOrEmpty(_InitialStr))
            {
                _Str = this.Mass_Inserted.Replace(_InitialStr, Str.ToString());
            }
            return _Str;
        }
        #endregion
        #region "新闻热链商品"
        /// <summary>
        /// 新闻热链商品
        /// </summary>
        /// <param name="newsID">新闻ID</param>
        /// <returns></returns>
        protected string Analyse_ProductHotForNews(int newsID)
        {
            InitialLabel();
            string ShowRoot = this.GetParamValue("NT:ShowRoot");
            string showCol = this.GetParamValue("NT:ShowCol");
            String titleLength = this.GetParamValue("NT:TitleLength");
            if (String.IsNullOrEmpty(showCol) || !Public.IsNumber(showCol))
            {
                showCol = "10";
            }

            DataTable Dt = new DataTable();

            Dt = CommonData.GetProductListByNewsID(newsID, Convert.ToInt16(showCol));
            StringBuilder Str = new StringBuilder();
            if (Dt!=null && Dt.Rows.Count > 0)
            {
                foreach (DataRow Row in Dt.Rows)
                {
                    string _str = _LoopStr;
                    _str = _str.Replace("{#ProductUrl}", string.Format("{0}ProductDetail_{1}_{2}.aspx", UIConfig.InstallDir, Row["ProductClassID"].ToString(), Row["ID"].ToString()));
                    if (!String.IsNullOrEmpty(titleLength) && Public.IsNumber(titleLength) )
                    {
                        _str = _str.Replace("{#ProductName}", BasePage.CutString(Convert.ToString(Row["ProductName"]), Convert.ToInt32(titleLength)));
                    }
                    else
                    {
                        _str = _str.Replace("{#ProductName}", Convert.ToString(Row["ProductName"]));
                    }
                    _str = _str.Replace("{#FullName}",Convert.ToString( Row["ProductName"]));
                    _str = _str.Replace("{#TradePrice}", Convert.ToString(Row["TradePrice"]));
                    _str = _str.Replace("{#MarketPrice}", Convert.ToString(Row["MarketPrice"]));
                    _str = _str.Replace("{#ImgProLoad}", "/UploadFile/Product/" + Convert.ToString(Row["ImgProLoad"]));
                    Str.Append(_str);
                }
                string _Str = string.Empty;
                if (!string.IsNullOrEmpty(_InitialStr))
                {
                    _Str = this.Mass_Inserted.Replace(_InitialStr, Str.ToString());
                }
                return _Str;
            }
            else
            {
                return null;
            }
        }
        #endregion
        #region "商品热链商品"
        /// <summary>
        /// 商品热链商品
        /// </summary>
        /// <param name="productID">商品ID</param>
        /// <returns></returns>
        protected string Analyse_ProductHot(int productID)
        {
            InitialLabel();
            string ShowRoot = this.GetParamValue("NT:ShowRoot");
            string showCol = this.GetParamValue("NT:ShowCol");
            String titleLength = this.GetParamValue("NT:TitleLength");
            if (String.IsNullOrEmpty(showCol) || !Public.IsNumber(showCol))
            {
                showCol = "10";
            }

            DataTable Dt = new DataTable();

            Dt = CommonData.GetProductListByPruductID(productID, Convert.ToInt16(showCol));
            StringBuilder Str = new StringBuilder();
            if (Dt!=null && Dt.Rows.Count > 0)
            {
                foreach (DataRow Row in Dt.Rows)
                {
                    string _str = _LoopStr;
                    _str = _str.Replace("{#ProductUrl}", string.Format("{0}ProductDetail_{1}_{2}.aspx", UIConfig.InstallDir, Convert.ToString(Row["ProductClassID"]), Convert.ToString(Row["ID"])));
                    if (!String.IsNullOrEmpty(titleLength) && Public.IsNumber(titleLength) )
                    {
                        _str = _str.Replace("{#ProductName}", BasePage.CutString(Convert.ToString(Row["ProductName"]), Convert.ToInt32(titleLength)));
                    }
                    else
                    {
                        _str = _str.Replace("{#ProductName}", Convert.ToString(Row["ProductName"]));
                    }
                    _str = _str.Replace("{#FullName}", Convert.ToString(Row["ProductName"]));
                    _str = _str.Replace("{#TradePrice}", Convert.ToString(Row["TradePrice"]));
                    _str = _str.Replace("{#MarketPrice}", Convert.ToString(Row["MarketPrice"]));
                    _str = _str.Replace("{#ImgProLoad}", "/UploadFile/Product/" + Convert.ToString(Row["ImgProLoad"]));
                    Str.Append(_str);
                }
                string _Str = string.Empty;
                if (!string.IsNullOrEmpty(_InitialStr))
                {
                    _Str = this.Mass_Inserted.Replace(_InitialStr, Str.ToString());
                }
                return _Str;
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region "商品浏览记录"
        /// <summary>
        /// 商品浏览记录
        /// </summary>
        /// <returns></returns>
        protected String Analyse_ProductHistory()
        {
            InitialLabel();
            string showCol = this.GetParamValue("NT:ShowCol");
            String titleLength = this.GetParamValue("NT:TitleLength");
            if (String.IsNullOrEmpty(showCol) || !Public.IsNumber(showCol))
            {
                showCol = "10";
            }
            StringBuilder Str = new StringBuilder();

            HttpCookie cookie = HttpContext.Current.Request.Cookies["History"];
            if (cookie != null && !String.IsNullOrEmpty(cookie.Value) && Public.IsNumericSet(cookie.Value))
            {

                DataTable Dt = new DataTable();

                Dt = CommonData.GetProductListByIDSet(cookie.Value, Convert.ToInt16(showCol));
                if (Dt != null && Dt.Rows.Count > 0)
                {
                    foreach (DataRow Row in Dt.Rows)
                    {
                        string _str = _LoopStr;
                        _str = _str.Replace("{#ProductUrl}", string.Format("{0}ProductDetail_{1}_{2}.aspx", UIConfig.InstallDir, Convert.ToString(Row["ProductClassID"]), Convert.ToString(Row["ID"])));
                        if (!String.IsNullOrEmpty(titleLength) && Public.IsNumber(titleLength))
                        {
                            _str = _str.Replace("{#ProductName}", BasePage.CutString(Convert.ToString(Row["ProductName"]), Convert.ToInt32(titleLength)));
                        }
                        else
                        {
                            _str = _str.Replace("{#ProductName}", Convert.ToString(Row["ProductName"]));
                        }
                        _str = _str.Replace("{#FullName}", Convert.ToString(Row["ProductName"]));
                        _str = _str.Replace("{#TradePrice}", Convert.ToString(Row["TradePrice"]));
                        _str = _str.Replace("{#MarketPrice}", Convert.ToString(Row["MarketPrice"]));
                        _str = _str.Replace("{#ImgProLoad}", "/UploadFile/Product/" + Convert.ToString(Row["ImgProLoad"]));
                        Str.Append(_str);
                    }
                }
            }

            String _Str = string.Empty;
            if (!String.IsNullOrEmpty(_InitialStr))
            {
                _Str = this.Mass_Inserted.Replace(_InitialStr, Str.ToString());
            }
            return _Str;
        }
        #endregion

        #region "商品列表分页"
        /// <summary>
        /// 商品列表分页
        /// </summary>
        /// <param name="ColumnID">栏目ID</param>
        /// <returns></returns>
        protected string Analyse_ProductListPage(int ColumnID)
        {
            InitialLabel();
            string ShowRoot = this.GetParamValue("NT:ShowRoot");
            string _ColumnID = this.GetParamValue("NT:ColumnID");
            string showCol = this.GetParamValue("NT:ShowCol");
            string productKey = this.GetParamValue("NT:ProductKey");
            string includeChild = this.GetParamValue("NT:IncludeChild");
            String titleLength = this.GetParamValue("NT:TitleLength");
            int pageNo = 0;
            if (String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["Page"]) || !Public.IsNumber(HttpContext.Current.Request.QueryString["Page"]))
            { pageNo = 1; }
            else
            {
                pageNo = Convert.ToInt16(HttpContext.Current.Request.QueryString["Page"]);
            }

            if (!string.IsNullOrEmpty(_ColumnID) && Public.IsNumber(_ColumnID))
            {
                ColumnID = Convert.ToInt32(_ColumnID);
            }

            Int32 pageSize=10;
            if(!String.IsNullOrEmpty(showCol) && Public.IsNumber(showCol))
            {
                pageSize=Convert.ToInt32(showCol);
            }

            DataTable dt = null;
            Int32 recordCount=0,pageCount=1;
            SQLConditionInfo[] param = new SQLConditionInfo[] { new SQLConditionInfo("ColumnID", ColumnID), new SQLConditionInfo("ProductKey", productKey) };
            if (includeChild == "True")
            {
                dt = CommonData.GetProductListWithChildColumn(pageNo, pageSize, out recordCount, out pageCount, param);
            }
            else
            {
                dt = CommonData.GetProductList(pageNo, pageSize, out recordCount, out pageCount, param);
            }

            if (dt == null || dt.Rows.Count <= 0) return String.Empty;

            StringBuilder Str = new StringBuilder();

            Regex c = new Regex(@"(<!--for-->(?<attributes>[^\]]*?)<!--/for-->)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            string cAtt = String.Empty;//存储属性
            string cAll = String.Empty;

            foreach (Match m in c.Matches(this.Mass_Inserted.ToString()))
            {
                //获取属性
                cAtt = m.Groups["attributes"].ToString();
                cAll = m.Groups[0].Value.ToString();

                string tempContent = String.Empty;
                foreach (DataRow Row in dt.Rows)
                {
                    string _str = cAtt.ToString();
                    _str = _str.Replace("{#ProductUrl}", string.Format("{0}ProductDetail_{1}_{2}.aspx", UIConfig.InstallDir,Convert.ToString(Row["ProductClassID"]), Convert.ToString(Row["ID"])));
                    if (!String.IsNullOrEmpty(titleLength) && Public.IsNumber(titleLength) )
                    {
                        _str = _str.Replace("{#ProductName}", BasePage.CutString(Convert.ToString(Row["ProductName"]), Convert.ToInt32(titleLength)));
                    }
                    else
                    {
                        _str = _str.Replace("{#ProductName}", Convert.ToString(Row["ProductName"]));
                    }
                    _str = _str.Replace("{#FullName}", Convert.ToString(Row["ProductName"]));
                    _str = _str.Replace("{#TradePrice}", Convert.ToString(Row["TradePrice"]));
                    _str = _str.Replace("{#MarketPrice}", Convert.ToString(Row["MarketPrice"]));
                    _str = _str.Replace("{#ImgProLoad}", "/UploadFile/Product/" + Convert.ToString(Row["ImgProLoad"]));
                    tempContent += _str;
                }
                this.Mass_Inserted = this.Mass_Inserted.Replace(cAll, tempContent);
            }

            string pageHtml = Paginator.Pagination(pageNo, recordCount, pageCount, "" + UIConfig.InstallDir + "ProductList_" + ColumnID + "");


            string _Str = string.Empty;
            this.Mass_Inserted = this.Mass_Inserted.Replace("{#PageInfo}", pageHtml);
            if (!string.IsNullOrEmpty(_InitialStr))
            {
                _Str = this.Mass_Inserted.Replace(_InitialStr, this.Mass_Inserted.ToString());
            }
            return _Str;
        }
        #endregion

        #region "商品搜索分页"
        /// <summary>
        /// 商品搜索分页
        /// </summary>
        /// <returns></returns>
        protected string Analyse_SearchPage()
        {
            InitialLabel();
            string ShowRoot = this.GetParamValue("NT:ShowRoot");
            string showCol = this.GetParamValue("NT:ShowCol");
            String titleLength = this.GetParamValue("NT:TitleLength");
            int pageNo = 0;
            if (String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["Page"]) || !Public.IsNumber(HttpContext.Current.Request.QueryString["Page"]))
            { pageNo = 1; }
            else
            {
                pageNo = Convert.ToInt16(HttpContext.Current.Request.QueryString["Page"]);
            }

            SQLConditionInfo[] param = new SQLConditionInfo[] { 
                new SQLConditionInfo("Max",Convert.ToString(HttpContext.Current.Request.Params["Max"])),//价格上限 
                new SQLConditionInfo("Min",Convert.ToString(HttpContext.Current.Request.Params["Min"])),//价格下限
                new SQLConditionInfo("c",Convert.ToString(HttpContext.Current.Request.Params["c"])),//栏目ID
                new SQLConditionInfo("b",Convert.ToString(HttpContext.Current.Request.Params["b"])),//品牌ID
                new SQLConditionInfo("t",Convert.ToString(HttpContext.Current.Request.Params["t"])),//产品类别
                new SQLConditionInfo("k",Convert.ToString(HttpContext.Current.Request.Params["k"])),//关键词
                new SQLConditionInfo("sv",Convert.ToString(HttpContext.Current.Request.Params["sv"])),//规格值ID
                new SQLConditionInfo("o",Convert.ToString(HttpContext.Current.Request.Params["o"])),//排序字段
                new SQLConditionInfo("or",Convert.ToString(HttpContext.Current.Request.Params["or"])),//排序规则，升序或者降序
                new SQLConditionInfo("tag",Convert.ToString(HttpContext.Current.Request.Params["tag"])),//商品标签
                new SQLConditionInfo("kw",Convert.ToString(HttpContext.Current.Request.QueryString["kw"]))//商品关键词
            };


            Int32 pageSize = 10;
            if (!String.IsNullOrEmpty(showCol) && Public.IsNumber(showCol))
            {
                pageSize = Convert.ToInt32(showCol);
            }

            DataTable dt = null;
            Int32 recordCount = 0, pageCount = 1;
            dt = CommonData.GetProductListWithCondition(pageNo, pageSize, out recordCount, out pageCount, param);

            StringBuilder Str = new StringBuilder();

            Regex c = new Regex(@"(<!--for-->(?<attributes>[^\]]*?)<!--/for-->)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            string cAtt = String.Empty;//存储属性
            string cAll = String.Empty;

            foreach (Match m in c.Matches(this.Mass_Inserted.ToString()))
            {
                //获取属性
                cAtt = m.Groups["attributes"].ToString();
                cAll = m.Groups[0].Value.ToString();

                string tempContent = String.Empty;
                foreach (DataRow Row in dt.Rows)
                {
                    string _str = cAtt.ToString();
                    _str = _str.Replace("{#ProductUrl}", string.Format("{0}ProductDetail_{1}_{2}.aspx", UIConfig.InstallDir, Row["ProductClassID"].ToString(), Row["ID"].ToString()));
                    if (!String.IsNullOrEmpty(titleLength) && Public.IsNumber(titleLength))
                    {
                        _str = _str.Replace("{#ProductName}", BasePage.CutString(Convert.ToString(Row["ProductName"]), Convert.ToInt32(titleLength)));
                    }
                    else
                    {
                        _str = _str.Replace("{#ProductName}", Convert.ToString(Row["ProductName"]));
                    }
                    _str = _str.Replace("{#FullName}", Convert.ToString(Row["ProductName"]));
                    _str = _str.Replace("{#TradePrice}", Convert.ToString(Row["TradePrice"]));
                    _str = _str.Replace("{#MarketPrice}", Convert.ToString(Row["MarketPrice"]));
                    _str = _str.Replace("{#ImgProLoad}", "/UploadFile/Product/" + Convert.ToString(Row["ImgProLoad"]));
                    tempContent += _str;
                }
                this.Mass_Inserted = this.Mass_Inserted.Replace(cAll, tempContent);
            }

            string pageHtml = Paginator.Pagination(pageNo, recordCount, pageCount);


            string _Str = string.Empty;
            this.Mass_Inserted = this.Mass_Inserted.Replace("{#PageInfo}", pageHtml);
            if (!string.IsNullOrEmpty(_InitialStr))
            {
                _Str = this.Mass_Inserted.Replace(_InitialStr, this.Mass_Inserted.ToString());
            }
            return _Str;
        }
        #endregion 
        #region "友情链接"
        /// <summary>
        /// 友情链接
        /// </summary>
        /// <returns></returns>
        protected string Analyse_FriendLink(int ColumnID)
        {
            InitialLabel();
            string ShowRoot = this.GetParamValue("NT:ShowRoot");
            string _ColumnID = this.GetParamValue("NT:ColumnID");
            string showCol = this.GetParamValue("NT:ShowCol");
            if (String.IsNullOrEmpty(showCol) || !Public.IsNumber(showCol))
            {
                showCol = "10";
            }
            if ((!string.IsNullOrEmpty(_ColumnID)) && (Public.IsNumber(_ColumnID)))
            {
                ColumnID = Convert.ToInt32(_ColumnID);
            }
            DataTable Dt = CommonData.GetFriendLinkList(ColumnID, Convert.ToInt16(showCol));
            StringBuilder Str = new StringBuilder();
            foreach (DataRow Row in Dt.Rows)
            {
                string _str = _LoopStr;
                _str = _str.Replace("{#FriendLinkName}", Row["LinkName"].ToString());
                _str = _str.Replace("{#FriendLinkURL}",  Row["LinkAddress"].ToString());
                _str = _str.Replace("{#FriendLinkPic}", Row["LinkPic"].ToString());
                Str.Append(_str);
            }
            string _Str = string.Empty;
            if (!string.IsNullOrEmpty(_InitialStr))
            {
                _Str = this.Mass_Inserted.Replace(_InitialStr, Str.ToString());
            }
            return _Str;
        }
        #endregion
        #region "文章列表"
        /// <summary>
        /// 文章列表
        /// </summary>
        /// <returns></returns>
        protected string Analyse_NewsList(int ColumnID)
        {
            InitialLabel();
            string ShowRoot = this.GetParamValue("NT:ShowRoot");
            string _ColumnID = this.GetParamValue("NT:ColumnID");
            string showCol = this.GetParamValue("NT:ShowCol");
            String start = this.GetParamValue("NT:Start");

            if (String.IsNullOrEmpty(showCol) || !Public.IsNumber(showCol))
            {
                showCol = "10";
            }
            if ((!string.IsNullOrEmpty(_ColumnID)) && (Public.IsNumber(_ColumnID)))
            {
                ColumnID = Convert.ToInt32(_ColumnID);
            }

            //起始位置确定
            Int32 i = 0;
            if (!String.IsNullOrEmpty(start) && Public.IsNumber(start))
            {
                if (Convert.ToInt32(start) > 0)
                {
                    i = Convert.ToInt32(start) - 1;
                }
            }

            DataTable Dt = CommonData.GetNewsList(ColumnID,Convert.ToInt16(showCol)+i);
            StringBuilder Str = new StringBuilder();

            if (Dt != null && Dt.Rows.Count > i)
            {
                DataRow Row = null;
                for (; i < Dt.Rows.Count; i++)
                {
                    Row = Dt.Rows[i];
                    String _str = _LoopStr;
                    _str = _str.Replace("{#NewsUrl}", String.Format("{0}NewsDetail_{1}_{2}.aspx", UIConfig.InstallDir, Row["NewsClassID"].ToString(), Row["ID"].ToString()));
                    _str = _str.Replace("{#NewsTitle}", Row["NewsTitle"].ToString());
                    _str = _str.Replace("{#PostData}", Convert.ToDateTime(Row["AddTime"]).ToString("yyyy-MM-dd"));
                    _str = _str.Replace("{#PostTime}", Convert.ToDateTime(Row["AddTime"]).ToString("HH:mm:ss"));
                    Str.Append(_str);
                }
            }
            String _Str = String.Empty;
            if (!String.IsNullOrEmpty(_InitialStr))
            {
                _Str = this.Mass_Inserted.Replace(_InitialStr, Str.ToString());
            }
            return _Str;
        }
        #endregion
        #region "文章列表分页"
        /// <summary>
        /// 文章列表分页
        /// </summary>
        /// <param name="ColumnID">当前栏目ID</param>
        /// <returns>解析后的单元Html</returns>
        protected String Analyse_NewsListPage(Int32 ColumnID)
        {
            InitialLabel();
            string ShowRoot = this.GetParamValue("NT:ShowRoot");
            string _ColumnID = this.GetParamValue("NT:ColumnID");
            string showCol = this.GetParamValue("NT:ShowCol");
            string includeChild = this.GetParamValue("NT:IncludeChild");
            int pageNo = 0;
            if (String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["Page"]) || !Public.IsNumber(HttpContext.Current.Request.QueryString["Page"]))
            { pageNo = 1; }
            else
            {
                pageNo = Convert.ToInt16(HttpContext.Current.Request.QueryString["Page"]);
            }

            if ((!string.IsNullOrEmpty(_ColumnID)) && (Public.IsNumber(_ColumnID)))
            {
                ColumnID = Convert.ToInt32(_ColumnID);
            }
            Int32 pageSize = 10;
            if (!String.IsNullOrEmpty(showCol) && Public.IsNumber(showCol))
            {
                pageSize = Convert.ToInt32(showCol);
            }

            DataTable dt = null;
            Int32 recordCount = 0, pageCount = 1;
            SQLConditionInfo[] param = new SQLConditionInfo[] { new SQLConditionInfo("ColumnID", ColumnID) };
            if (includeChild == "True")
            {
                dt = CommonData.GetNewsListWithChildColumn(pageNo, pageSize, out recordCount, out pageCount, param);
            }
            else
            {
                dt = CommonData.GetNewsList(pageNo, pageSize, out recordCount, out pageCount, param);
            }

            if (dt == null || dt.Rows.Count <= 0) return String.Empty;

            StringBuilder Str = new StringBuilder();

            Regex c = new Regex(@"(<!--for-->(?<attributes>[^\]]*?)<!--/for-->)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            string cAtt = String.Empty;//存储属性
            string cAll = String.Empty;

            foreach (Match m in c.Matches(this.Mass_Inserted.ToString()))
            {
                //获取属性
                cAtt = m.Groups["attributes"].ToString();
                cAll = m.Groups[0].Value.ToString();

                string tempContent = String.Empty;
                foreach (DataRow Row in dt.Rows)
                {
                    string _str = cAtt.ToString();
                    _str = _str.Replace("{#NewsUrl}", string.Format("{0}NewsDetail_{1}_{2}.aspx", UIConfig.InstallDir,Convert.ToString(Row["NewsClassID"]), Convert.ToString(Row["ID"])));
                    _str = _str.Replace("{#NewsTitle}", Convert.ToString(Row["NewsTitle"]));
                    _str = _str.Replace("{#PostData}", Convert.ToDateTime(Row["AddTime"]).ToString("yyyy-MM-dd"));
                    _str = _str.Replace("{#PostTime}", Convert.ToDateTime(Row["AddTime"]).ToString("HH:mm:ss"));
                    tempContent += _str;
                }
                this.Mass_Inserted = this.Mass_Inserted.Replace(cAll, tempContent);
            }

            string pageHtml = Paginator.Pagination(pageNo, recordCount, pageCount, "" + UIConfig.InstallDir + "NewsList_" + ColumnID + "");


            string _Str = string.Empty;
            this.Mass_Inserted = this.Mass_Inserted.Replace("{#PageInfo}", pageHtml);
            if (!string.IsNullOrEmpty(_InitialStr))
            {
                _Str = this.Mass_Inserted.Replace(_InitialStr, this.Mass_Inserted.ToString());
            }
            return _Str;
        }
        #endregion
        #region "文章正文"
        /// <summary>
        /// 文章正文
        /// </summary>
        /// <param name="ColumnID"></param>
        /// <param name="NewsID"></param>
        /// <returns></returns>
        private string Analyse_ReadNews(int ColumnID,int NewsID)
        {
            InitialLabel();
            return this.Mass_Inserted;
        }
        #endregion
        #region "商品详细页面"
        /// <summary>
        /// 商品详细页面
        /// </summary>
        /// <param name="ColumnID"></param>
        /// <param name="NewsID"></param>
        /// <returns></returns>
        private string Analyse_ReadProduct(int ColumnID, int NewsID)
        {
            InitialLabel();
            return this.Mass_Inserted;
        }
        #endregion

        #region "品牌列表"
        /// <summary>
        /// 品牌列表
        /// </summary>
        /// <returns></returns>
        protected string Analyse_BrankList()
        {
            InitialLabel();
            string showCol = this.GetParamValue("NT:ShowCol");
            if (String.IsNullOrEmpty(showCol) || !Public.IsNumber(showCol))
            {
                showCol = "10";
            }
            DataTable Dt = new DataTable();
            Dt = CommonData.GetBrankList(Convert.ToInt32(showCol));
            if (Dt != null && Dt.Rows.Count > 0)
            {
                StringBuilder Str = new StringBuilder();
                foreach (DataRow Row in Dt.Rows)
                {
                    string _str = _LoopStr;
                    _str = _str.Replace("{#BrankID}",Convert.ToString(Row["ID"]));
                    _str = _str.Replace("{#BrankName}", Convert.ToString(Row["BrankName"]));
                    _str = _str.Replace("{#BrankAddress}", Convert.ToString(Row["BrankAddress"]));
                    _str = _str.Replace("{#BrankOtherName}", Convert.ToString(Row["BrankOtherName"]));
                    _str = _str.Replace("{#BrankDetail}", Convert.ToString(Row["BrankDetail"]));
                    _str = _str.Replace("{#BrankPic}", Convert.ToString(Row["BrankPic"]));
                    _str = _str.Replace("{#OrderID}", Convert.ToString(Row["OrderID"]));
                    Str.Append(_str);
                }
                string _Str = string.Empty;
                if (!string.IsNullOrEmpty(_InitialStr))
                {
                    _Str = this.Mass_Inserted.Replace(_InitialStr, Str.ToString());
                }
                return _Str;
            }
            else
            {
                return String.Empty;
            }
        }
        #endregion

        #region "广告设置"
        protected string Analyse_AdSet(int ColumnID)
        {
            InitialLabel();
            string ShowRoot = this.GetParamValue("NT:ShowRoot");
            string _ColumnID = this.GetParamValue("NT:ColumnID");
            string showCol = this.GetParamValue("NT:ShowCol");
            if (String.IsNullOrEmpty(showCol) || !Public.IsNumber(showCol))
            {
                showCol = "10";
            }
            if ((!string.IsNullOrEmpty(_ColumnID)) && (Public.IsNumber(_ColumnID)))
            {
                ColumnID = Convert.ToInt32(_ColumnID);
            }
            DataTable Dt = CommonData.GetAdsList(ColumnID, Convert.ToInt16(showCol));
            StringBuilder Str = new StringBuilder();
            foreach (DataRow Row in Dt.Rows)
            {
                string _str = _LoopStr;
                _str = _str.Replace("{#AdName}", Row["AdName"].ToString());
                string[] AdPicArray = Row["AdPic"].ToString().Split(Convert.ToChar("|"));
                if (AdPicArray.Length > 1)
                {
                    _str = _str.Replace("{#AdPic}", Public.GetSecond(Row["AdPic"].ToString()));
                }
                else
                {
                    _str = _str.Replace("{#AdPic}", Row["AdPic"].ToString());
                }
                _str = _str.Replace("{#AdAddress}", Row["AdAddress"].ToString());
                _str = _str.Replace("{#AdText}", Row["AdText"].ToString());
                Str.Append(_str);
            }
            string _Str = string.Empty;
            if (!string.IsNullOrEmpty(_InitialStr))
            {
                _Str = this.Mass_Inserted.Replace(_InitialStr, Str.ToString());
            }
            return _Str;
        }
        #endregion

    }
}
